/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.sanerzone.smscenter.modules.sms.service;

import com.sanerzone.jeebase.common.persistence.Page;
import com.sanerzone.jeebase.common.service.CrudService;
import com.sanerzone.jeebase.common.utils.StringUtils;
import com.sanerzone.smscenter.biz.iface.SMSGatewayInterface;
import com.sanerzone.smscenter.biz.iface.SmsConfigInterface;
import com.sanerzone.smscenter.modules.sms.dao.SmsGatewayDao;
import com.sanerzone.smscenter.modules.sms.entity.SmsGateway;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * 通道信息Service
 * @author zhukc
 * @version 2017-06-29
 */
@Service
@Transactional(readOnly = true)
public class SmsGatewayService extends CrudService<SmsGatewayDao, SmsGateway> {

	@Autowired
	private SmsConfigInterface smsConfigInterfaceImpl;

	@Autowired
	private SMSGatewayInterface smsGatewayInterface;

	public SmsGateway get(String id) {
		return super.get(id);
	}

	public List<SmsGateway> findList(SmsGateway smsGateway) {
		return super.findList(smsGateway);
	}

	public Page<SmsGateway> findPage(Page<SmsGateway> page, SmsGateway smsGateway) {
		return super.findPage(page, smsGateway);
	}

	@Transactional(readOnly = false)
	public void save(SmsGateway smsGateway) {
		super.save(smsGateway);
	}

	public boolean remoteSaveGateway(SmsGateway smsGateway, SmsGateway smsGatewayDB) {
		int deliverStatus = 0;
		int reportStatus = 0;
		int protoStatus = 0;
		if (smsGatewayDB == null) {
			protoStatus = 1;
			if (StringUtils.equals(smsGateway.getGwDeliverStatus(), "1")) {
				deliverStatus = 1;
			}
			if (StringUtils.equals(smsGateway.getGwReceiveModel(), "0")) {
				reportStatus = 1;
			}
		} else {
			if (!StringUtils.equals(smsGateway.getGwProtoClass(), smsGatewayDB.getGwProtoClass())
					|| !StringUtils.equals(smsGateway.getGwProtoExtparam(), smsGatewayDB.getGwProtoExtparam())) {
				protoStatus = 1;
			}
			if (!StringUtils.equals(smsGateway.getGwDeliverStatus(), smsGatewayDB.getGwDeliverStatus())
					&& StringUtils.equals(smsGatewayDB.getGwDeliverStatus(), "0")) {
				deliverStatus = 1;
			}
			if (!StringUtils.equals(smsGateway.getGwReceiveModel(), smsGatewayDB.getGwReceiveModel())
					&& StringUtils.equals(smsGatewayDB.getGwReceiveModel(), "0")) {
				reportStatus = 1;
			}
		}
		com.sanerzone.smscenter.biz.entity.SmsGateway entity = new com.sanerzone.smscenter.biz.entity.SmsGateway();
		BeanUtils.copyProperties(smsGateway,entity);
		return updateGatewayInfo(entity,protoStatus,reportStatus,deliverStatus);
	}
	@Transactional(readOnly = false)
	public void delete(SmsGateway smsGateway) {
		super.delete(smsGateway);
	}

	private void dobboGateway(String gwCode){
		com.sanerzone.smscenter.biz.entity.SmsGateway entity = dao.findBizEntity(gwCode);
		smsConfigInterfaceImpl.configGatewayInfo(1, entity, gwCode);
	}

	/**
	 * 网关类或者参数改变，修改相应缓存
	 */
	private boolean updateGatewayInfo(com.sanerzone.smscenter.biz.entity.SmsGateway entity,int protoStatus, int reportStatus, int deliverStatus){
		 return smsGatewayInterface.updateGW(entity, reportStatus,deliverStatus);
	}

	public SmsGateway getSmsGatewayByCode(SmsGateway smsGateway){
		return dao.getSmsGatewayByCode(smsGateway);
	}
}